import { nativeVerify } from './util';

// 开始原生调用
const startNativeVerify = (cmsConfig) => {
  // if (cmsConfig.Common.IsWxNative && !cmsConfig.Common.Flow.includes('Ocr') && cmsConfig.Common.IsHideIndexWhenNative) {
  console.log('直接调用微信原生接口')
  let WxVerifyTypeIsVideo = cmsConfig.Common.WxVerifyTypeIsVideo
  let tempWxCheckAliveType = cmsConfig.Common.WxCheckAliveType
  nativeVerify(
    WxVerifyTypeIsVideo,
    tempWxCheckAliveType,
    uni.faceidVerifySdkBaseUrl,
    uni.faceidVerifySdkToken ,
    exitVerifyFail,
    res => {
      let tempRes = {
        BizToken: uni.faceidVerifySdkToken ,
        ErrorCode: res.ErrorCode,
        ErrorMsg: res.ErrorMsg
      }
      if (res.ErrorCode === 0) {
        console.log('验证成功')
        // 验证成功
        exitVerify(tempRes)
      } else {
        console.log('验证失败')
        // 验证失败
        exitVerifyFail(tempRes)
      }
    },
    () => {
      console.log('verifySuccessFunc')
    }
  )
};

 // success verify exit, 传入一个对象方便追加传值
const exitVerify = (data) => {
  console.log('uni.faceidVerifySdkPageNum',uni.faceidVerifySdkPageNum)
  data.BizToken = uni.faceidVerifySdkToken 
  data.ErrorCode = 0
  const endPath = uni.faceidVerifySdkCmsConfig.Common.endPath ;
  console.log('endPath',endPath)
  if (endPath) {
    uni.redirectTo({
      url:endPath,
      success: () => {
        uni.verifySuccessFunc(data)
      },
      fail: err => {
        console.log('err',err)
      }
    })
  } else {
    uni.navigateBack({
      delta: uni.faceidVerifySdkPageNum,
      success: (e) => {
        console.log('uni.verifySuccessFunc',uni.verifySuccessFunc)
        uni.verifySuccessFunc(data)
      },
      fail: err => {
        console.log('err',err)
      }
    })
  }
}

const exitVerifyFail = (data) => {
  console.log('exitVerifyFail',data)

  data.BizToken = uni.faceidVerifySdkToken 
  const endPath = uni.faceidVerifySdkCmsConfig.Common.endPath ;
  // 如果是跳到指定的路径
  if (endPath) {
    if (data.ErrorCode === -999) {
      uni.redirectTo({
        url:endPath,
        success: () => {},
        fail: err => {
          console.log('err',err)
        }
      })
    } else {
      uni.verifyFailureFunc(data)
    }
  } else {
    uni.navigateBack({
      delta: uni.faceidVerifySdkPageNum,
      success: () => {
        if (data.ErrorCode !== -999) {
          uni.verifyFailureFunc(data)
        }
      },
      fail: err => {
        console.log('err',err)
      }
    })
  }
}

export {
  startNativeVerify,
  exitVerify,
  exitVerifyFail
}